import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
import org.xml.sax.AttributeList;
import org.xml.sax.DocumentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import java.io.*;
import java.util.List;

public class Dom4j_ {
    /**
     * 演示如何加载 XML 文件
     */
    @Test
    public void loadXML() throws DocumentException {
        //得到一个解析器
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/Main/Java/students2.xml"));
        //通过debug看看 document 对象的属性
        System.out.println(document);
    }

    /**
     * 遍历所有的 student 信息
     */
    @Test
    public void listStus() throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/Main/Java/students2.xml"));
        //1. 得到 rootElement
        Element rootElement = document.getRootElement();
        //2. 得到 rootElement 的 student Elements
        List<Element> students = rootElement.elements("student");
        //System.out.println(students.size());   2
        for (Element student : students) {
            //获取 Student 元素的 name Element
            Element name = student.element("name");
            Element age = student.element("age");
            Element resume = student.element("resume");
            Element gender = student.element("gender");
            System.out.println("学生信息= " + name.getText() + " " + age.getText() + " " + resume.getText() + " " + gender.getText());
        }
    }

    /**
     * 指定某个节点读取  就是 dom4j+xpath
     */
    @Test
    public void readOne() throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/Main/Java/students2.xml"));
        //1. 获取 rootElement 节点
        Element rootElement = document.getRootElement();
        //2. 获取第一个学生
        Element student = (Element) rootElement.elements("student").get(0);
        //3. 输出信息
        System.out.println("该学生的信息= " + student.element("name").getText() +" "+
                student.element("age").getText() + " " +student.element("resume").getText() +
                student.element("gender").getText());
        //4. 获取 student 元素的属性
        System.out.println("id = " +student.attributeValue("id"));
    }

    /**
     * 加元素 （要求：添加一个学生到xml中）
     */
    @Test
    public void add() throws DocumentException, IOException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/Main/Java/students2.xml"));
        //首先来创建一个学生节点对象
        Element newStu = document.getRootElement().addElement("student");
        Element id = newStu.addAttribute("id", "03");
        newStu.addElement("name").setText("宋江");
        newStu.addElement("age").setText("19");
        newStu.addElement("resume").setText("梁山老大");
        OutputFormat output = OutputFormat.createPrettyPrint();
        output.setEncoding("utf-8");//输出的编码 utf-8
        XMLWriter writer = new XMLWriter(new FileOutputStream(new File("src/Main/Java/students2.xml")), output);
        writer.write(document);
        writer.close();
    }
    /**
     * 删除元素（要求：删除第3个学生）
     */
    @Test
    public void del() throws DocumentException, IOException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/Main/Java/students2.xml"));
        Element stu = (Element) document.getRootElement().elements("student").get(0);
        //删除元素
        stu.getParent().remove(stu);
        OutputFormat output = OutputFormat.createPrettyPrint();
        output.setEncoding("utf-8");
        XMLWriter writer = new XMLWriter(new FileOutputStream(new File("src/Main/Java/students2.xml")), output);
        writer.write(document);
        writer.close();
        System.out.println("删除成功~");
    }
}
